# -*- coding: utf-8 -*-
import scrapy
# import re

# from scrapy.utils.project import get_project_settings
from scrapy_ndd.utils.common import get_conf, set_conf
from scrapy.loader import ItemLoader
from scrapy_ndd.items import ScrapyNddNewUrlItem


class PPXNewUrlSpider(scrapy.Spider):
    name = 'ppx_new_url'
    settings = get_conf()
    ppx_host = settings.get('PPX_HOST', '')
    allowed_domains = ['bitbucket.org']
    start_urls = ['https://bitbucket.org/51ppx/-/wiki/Home']

    def parse(self, response):
        item_loader = ItemLoader(item=ScrapyNddNewUrlItem(), response=response)
        item_loader.add_css("new_url", "h1 strong a::attr(href)")

        ndd_item = item_loader.load_item()
        set_conf(ndd_item.get('new_url', ''))
        yield ndd_item
